package com.kk.goGame.test;
/*
 *@author KK on 2023.02.17 21:34
 */

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;

class AlipayUtil {
    // 沙箱环境支付宝网关
    private static final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    // 应用ID
    private static final String APP_ID = "应用ID";
    // 应用私钥
    private static final String APP_PRIVATE_KEY = "应用私钥";
    // 支付宝公钥
    private static final String ALIPAY_PUBLIC_KEY = "支付宝公钥";

    public static String recharge(String outTradeNo, String totalAmount, String subject) {
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, "json", "utf-8", ALIPAY_PUBLIC_KEY, "RSA2");
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl("http://localhost:8080/payReturnUrl");
        alipayRequest.setNotifyUrl("http://localhost:8080/payNotifyUrl");

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
                + "\"total_amount\":\"" + totalAmount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String form = "";
        try {
            AlipayTradePagePayResponse alipayResponse = alipayClient.pageExecute(alipayRequest);
            form = alipayResponse.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return form;
    }
}

